<?php
if(@$_POST['open']!='please'){
	exit;
}
include_once("../../php/include_all.php");
?>
<div class="row-fluid sortable">
	<div class="box span12 geo-box">
		<div class="widget-header widget-header-flat ">
			<h5><i class="icon-calendar"></i> Master Kegiatan</h5>
		</div>
		<div class="box-content">
			<a href="#modalwin" data-toggle="modal" class="btn btn-small btn-primary2 no-radius btn-tambah"><i class="icon-plus"></i>Tambah Data</a>
			&nbsp;<span id="load" class="spinner"></span>
			<br/>
			<br/>
			<div id="modalwin" class="modal hide fade" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
				<header class="modal-header">
					<a href="#" class="close geo-clear" data-dismiss="modal">x</a>
					<h3>Kegiatan</h3>
				</header>
		
				<div class="modal-body">
					<form id="form_kegiatan">
						<input type="hidden" name="act" value="save_kegiatan">
						<input type="hidden" id="id_kegiatan" name="id_kegiatan" value="0">
						<table class="table-form" width="100%">
							<tbody>
								<tr>
									<td valign="middle" width="120px">Nama Klasifikasi</td>
									<td valign="">:</td>
									<td colspan="5">
										<select name="id_klasifikasi" id="id_klasifikasi">
											<option value="0" id="idklasi">- Pilih Klasifikasi -</option>
											<?php
											$klasifikasi = get_datas("select id_klasifikasi,id_urusan,kode_klasifikasi,nama_klasifikasi from mus_klasifikasi order by id_klasifikasi");
											foreach($klasifikasi as $klasifikasi){
											?>
											<option value="<?php echo $klasifikasi['id_klasifikasi']; ?>"><?php echo $klasifikasi['nama_klasifikasi']; ?></option>
											<?php
											}
											?>
										</select>
									</td>
								</tr>
								<tr>
									<td>Nama Program</td>
									<td>:</td>
									<td colspan="5">
										<select name="id_program" id="id_program">
											<option value="0" id="idpro">- Pilih Program -</option>
										</select>
									</td>
								</tr>
								<tr>
									<td>Kode Kegiatan</td>
									<td>:</td>
									<td colspan="5">
										<input type="text" name="kode_kegiatan" id="kode_kegiatan" class="span7" readonly="">
									</td>
								</tr>
								<tr>
									<td>Nama Kegiatan</td>
									<td>:</td>
									<td colspan="5">
										<!--input type="text" name="nama_kegiatan" class="span12"-->
										<textarea name="nama_kegiatan" id="nama_kegiatan" class="span12"></textarea>
									</td>
								</tr>
								<!--tr>
									<td valign="midle" rowspan="2">Scoring</td>
									<td valign="midle" rowspan="2">:</td>
									<td align="center">Dampak</td>
									<td align="center">Sasaran</td>
									<td align="center">Pelaksanaan</td>
									<td align="center">Biaya</td>
									<td align="center">Waktu</td>
								</tr>
								<tr>
									<td><input type="text" class="span12" name="dampak"></td>
									<td><input type="text" class="span12" name="sasaran"></td>
									<td><input type="text" class="span12" name="pelaksanaan"></td>
									<td><input type="text" class="span12" name="biaya"></td>
									<td><input type="text" class="span12" name="waktu"></td>
								</tr-->
								<tr>
									<td></td>
									<td></td>
									<td colspan="5">
										<a class="btn btn-primary2 btn-small bt-simpan-kegiatan"><div id="loader" style="margin-top:2px"></div><i class="icon-save"></i>&nbsp;Simpan</a><span id="result"></span>
										<span id="result"></span>
									</td>
								</tr>
							</tbody>
						</table>
					</form>
				</div>
			</div>
			
			<table class="table table-bordered geo-table">
				<thead>
					<tr>
						<td class="center" width="5%">No </td>
						<td class="center" width="15%">Kode</td>
						<td class="center" width="60%">Nama Kegiatan</td>
						<td class="center" width="25px">Action</td>
					</tr>
				</thead>
				<tbody id="tbody_kegiatan">
					<?php
					$kegiatan = get_datas("select * from mus_kegiatan order by kode_kegiatan");
					$no=1;
					foreach($kegiatan as $kegiatan){
					?>
					<tr>
						<td class="center"><?php echo $no  ?></td>
						<td><?php echo $kegiatan['kode_kegiatan']; ?></td>
						<td><?php echo $kegiatan['nama_kegiatan']; ?></td>
						<td class="center">
							<!--a class="btn btn-info bt-edit-kegiatan" name="<?php echo $kegiatan['id_keg']; ?>">Edit</a-->
							<a href="#modalwin" data-toggle="modal" class="btn btn-info btn-small bt-edit-kegiatan" name="<?php echo $kegiatan['id_keg']; ?>"><i class="icon-edit icon-white"></i>Ubah</a>
							<a class="btn btn-danger btn-small bt-delete-kegiatan" name="<?php echo $kegiatan['id_keg']; ?>"><i class="icon-trash icon-white"></i>Hapus</a>
						</td>
					</tr>
					<?php
					$no++;
					}
					?>
				</tbody>
			</table>
		</div>
	</div>
</div>
<script>
var urls = 'admin/master_kegiatan/kegiatan_save.php';

$('.bt-simpan-kegiatan').click(function(){
	var btn  = $(this);
	var load = $('#loader');
	var rslt = $('#result');
	 load.addClass('icon-spinner icon-spin icon-2x white');
//	btn.removeClass('btn-primary').addClass('btn-info');
	
	var form = $('#form_kegiatan');
	var data = form.serializeArray();
	var post = $.post(urls,data);
	post.done(function(res){
		var result = res.split('__');
		//   0      1     2 
		//success__idkeg__(isi tbody)tr
		if(result.length==3){
			if(result[0]=='success'){
				rslt.html('<font color="green">Data tersimpan...</font>');
				setTimeout(function(){
					rslt.html('');
				},1500);
				
				var tbody = $('#tbody_kegiatan');
				tbody.html(result[2]);
				init();
				$("#id_kegiatan").val("0");
		 		document.getElementById("id_klasifikasi").selected = true;
			//	document.getElementById("idpro").selected = true;
		 		$("#kode_kegiatan").val(" ");
		 		$("#nama_kegiatan").val(" ");
				$('#modalwin').modal('hide');
			 
			}else{
				rslt.html('<font color="red">'+res+'</font>');
			}
		}else{
			rslt.html('<font color="red">'+res+'</font>');
		}
		load.removeClass();
		btn.removeClass('btn-info').addClass('btn-primary');
	});
});
	
$('.btn-tambah').click(function(){
	$("#id_kegiatan").val("0");
	document.getElementById("idklasi").selected = true;
//	document.getElementById("idpro").selected = true;
	$('#id_program').html('<option value="0">- Pilih Program -</option>');
//	$('#id_kegiatan').val(0);
	$("#kode_kegiatan").val(" ");
	$("#nama_kegiatan").val(" ");
});
	
function init(){
	$('.bt-delete-kegiatan').click(function(){
		var btn  = $(this);
		var pid  = this.name;
		var load = $(this);
		 load.html('<div class="icon-spinner icon-spin icon-2x white"></div>');
         bootbox.confirm("Anda akan menghapus kegiatan ini?", function(result) {
			if(result==true){
				if(pid!=0){
					var post = $.post(urls,{act:"del_kegiatan",idkeg:pid});
					post.done(function(data){
						var hasil = data.split('__');
						if(hasil.length==2){
							if(hasil[0]=='success'){
						//	btn.parent().parent().remove();
							var tbody = $('#tbody_kegiatan');
								tbody.html(hasil[1]);
								init();
							}
						}
					});
					load.html('<i class="icon-trash icon-white"></i> Hapus</a> ');
				}
			}else{
				load.html('<i class="icon-trash icon-white"></i> Hapus</a> ');
			}
		});
	});
	
	$('.bt-edit-kegiatan').click(function(){
		var idkeg= this.name;
		var form = $('#form_kegiatan');
		var post = $.post(urls,{act:'edit_kegiatan',idkeg:idkeg});
		post.done(function(res){
			var value = res.split('__');
			
			//  0          1             2             3          4           5
			//id_keg__id_program__kode_kegiatan__nama_kegiatan__id_kla__(list_program)
			$('#id_kegiatan').val(value[0]);
			form.find('select[name="id_klasifikasi"]').val(value[4]);
			form.find('select[name="id_program"]').html(value[5]);
			form.find('select[name="id_program"]').val(value[1]);
			form.find('input[name="kode_kegiatan"]').val(value[2]);
			form.find('textarea[name="nama_kegiatan"]').val(value[3]);
			//console.log(res);
		});
	});
}

$('#id_klasifikasi').change(function(){
	var post = $.post(urls,{act:'get_program',idkla:this.value});
	post.done(function(data){
		$('#id_program').html(data);
	});
});

$('#id_program').change(function(){
	var post = $.post(urls, {act:'get_kode', kode:this.value});
	post.done(function(data){
	//	$('#kode_kegiatan').html(data);
		document.getElementById('kode_kegiatan').value=data;
	});
});

/*
$('.geo-clear').click(function(){
	$('#id_program').html('<option value="0">- Pilih Program -</option>');
	$('#id_kegiatan').val(0);
	document.getElementById('form_kegiatan').reset();
});
*/
init();
</script>